<template>
  <div class="vehicle-schedule">
    <div class="card vehicle-schedule__search-bar">
      <el-form inline>
        <el-form-item label="使用时间：">
          <el-date-picker
            v-model="params.time"
            placeholder="选择日期"
            type="date"
            value-format="yyyy-MM-dd"
          />
        </el-form-item>
        <el-form-item label="预约车辆：">
          <el-select
            v-model="params.vehicle_resources_id"
            placeholder="请选择"
            clearable
          >
            <el-option
              v-for="item in vehiclesList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="handleReset">重置</el-button>
          <el-button type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card">
      <VehicleScheduleList
        :schedule-list="list"
        :date="params.time"
      />

      <div class="vehicle-schedule__page">
        <Pagination
          style="margin-top: 0; padding-bottom: 0;"
          :total="total"
          :page="params.page"
          :limit="params.paginate"
          @pagination="pagination"
        />
      </div>
    </div>
  </div>
</template>
<script>
import { getVehicleScheduleList, getVehiclesList } from '@/api/vehicleService'
import VehicleScheduleList from '@/views/vehicleService/components/vehicleSchedule/ScheduleList.vue'
import Pagination from '@/components/Pagination/index.vue'

export default {
  name: '',
  components: { Pagination, VehicleScheduleList },
  data() {
    return {
      params: {
        time: '',
        vehicle_resources_id: null,
        page: 1,
        paginate: 10
      },
      list: [],
      vehiclesList: [],
      total: 0
    }
  },

  created() {
    this.getScheduleList()
    this.getVehiclesListFetch()
  },

  methods: {
    handleSearch() {
      this.getScheduleList()
    },

    handleReset() {
      this.params = this.$options.data().params
      this.getScheduleList()
    },

    async getScheduleList() {
      try {
        const res = await getVehicleScheduleList({
          ...this.params,
          riqi: this.params.time
        })
        this.list = res.data.data
        this.total = res.data.total
      } catch (error) {
        this.$message.error(error.message || '信息获取失败')
      }
    },

    async getVehiclesListFetch() {
      try {
        const res = await getVehiclesList()
        this.vehiclesList = res.data.map(item => {
          return {
            value: item?.id,
            label: item?.license_plate_number ? `${item?.type?.name}-${item?.license_plate_number}` : item?.type?.name
          }
        })
      } catch (error) {
        throw new Error(error.message || '车辆数据获取失败')
      }
    },

    pagination(page) {
      this.params.page = page.page
      this.params.paginate = page.limit
      this.getScheduleList()
    }
  }
}
</script>
<style lang="scss" scoped>
.vehicle-schedule {
  padding: 20px;

  &__search-bar {
    ::v-deep(.el-form-item) {
      margin-bottom: 0;
      margin-right: 20px;
      .el-form-item__label {
        font-weight: 400;
      }
    }
  }

  &__page {
    display: flex;
    justify-content: flex-end;
  }

}
</style>
